﻿#define mode1


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NFox;
using Autodesk.AutoCAD.ApplicationServices;
using Autodesk.AutoCAD.Runtime;
using System.Windows.Interop;
using PInvoke;
using Autodesk.AutoCAD.Windows;
using LabelBar.View;
using System.Runtime.InteropServices;
using System.Windows;
using Application = Autodesk.AutoCAD.ApplicationServices.Application;
using System.Windows.Forms;
using System.CodeDom;

namespace LabelBar
{
  
    public class Funcommand : IExtensionApplication
    {
        MainView window;
        proc_handle p1;
        public void setwindow()  //设置窗口
        {
          


            try
            {
                window.Show();               
                POINT pt = new POINT();
                PInvoke.User32.ScreenToClient(Application.DocumentManager.MdiActiveDocument.Window.Handle, ref pt);
                window.Left = pt.x;
                window.Top = pt.y;
                var handle = new WindowInteropHelper(window).Handle;
                new WindowInteropHelper(window).Owner = Application.MainWindow.Handle;
               // PInvoke.User32.SetParent(handle, Application.DocumentManager.MdiActiveDocument.Window.Handle);
            }
            catch (System.Exception)
            {

              
            }
         

        }

        public Funcommand()
        {
            // window = new MainView(Application.MainWindow,Application.DocumentManager.MdiActiveDocument.Window); // CAD不会崩溃
            window = new MainView();
            Application.DocumentManager.DocumentDestroyed += DocumentManager_DocumentDestroyed;
            Application.DocumentManager.DocumentActivationChanged += DocumentManager_DocumentActivationChanged;
            Application.DocumentManager.DocumentActivated += DocumentManager_DocumentActivated;

            setwindow();
           p1=  new proc_handle(window);
           p1.AssignHandle(Application.MainWindow.Handle);

        }

        private void DocumentManager_DocumentDestroyed(object sender, DocumentDestroyedEventArgs e)
        {
            window.Close();
        }

        private void DocumentManager_DocumentActivated(object sender, DocumentCollectionEventArgs e)
        {
           // setwindow();
        }

        private void DocumentManager_DocumentActivationChanged(object sender, DocumentActivationChangedEventArgs e)
        {
           // setwindow();
        }

        public void Initialize()
        {
            NFox.Cad.EditorEx.WriteMessage("\nMVVM test starting ");
           // window = new MainView(); // CAD会崩溃
        }

   

        public void Terminate()
        {
            
        }
    }

    class proc_handle :NativeWindow
    {
        private MainView mv;
        public proc_handle(MainView win)
        {
            mv = win;
        }
        protected override void WndProc(ref Message m)
        {
            try
            {
                if (m.Msg == (int)PInvoke.User32.WindowMessage.WM_SIZE | m.Msg == (int) User32.WindowMessage.WM_MOVE) // WM_SIZE
                {
                    POINT pt = new POINT();
                    User32.ClientToScreen(Application.DocumentManager.MdiActiveDocument.Window.Handle, ref pt);
                    User32.GetWindowRect(Application.DocumentManager.MdiActiveDocument.Window.Handle, out RECT rec);
                    mv.Left = pt.x;
                    mv.Top = pt.y;
                    mv.Width = rec.right - rec.left;
                }
                base.WndProc(ref m);//回调函数    
            }
            catch //cad崩溃的时候会触发
            { }
        }
    }

}
